package com.example.dellpc.myapplication;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by dellpc on 2016/8/10.
 */
public class TitleFragment extends Fragment {

    private TextView tv_title;
    private String  name;

    private boolean isVisible;

    public TitleFragment(String name) {
        this.name = name;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.titlefragment, container, false);
        initView(v);
        return v;
    }

    public void initView(View view) {
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setBackgroundColor(Color.WHITE);
        tv_title.setText(name);
//        Intent intent = getActivity().getIntent();
//        if (intent != null) {
//            Bundle bundle = intent.getExtras();
//            String name = bundle.getString("title");
//            tv_title.setText(name);
//        }

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()){
            isVisible=true;
            onVisible();
        }else {
            isVisible=false;
            onInvisible();
        }
    }
/**
    * 当界面呈现给用户，即设置可见时执行，进行加载数据的方法
    * 在用户可见时加载数据，而不在用户不可见的时候加载数据，是为了防止控件对象出现空指针异常
    */
    protected void onVisible(){
        setlazyLoad();
    }
    /**
     * 当界面还没呈现给用户，即设置不可见时执行
     */
    protected void onInvisible(){
    }
    /**
     * 加载数据方法
     */
    protected void setlazyLoad(){
    }
}
